var app=getApp();
var ux = require('../../../../lib/ux.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {    
    mobile:'',
    verifyCodeTime: '获取验证码',
    verifyCode: '',
    buttonDisable: false,
    code:'',
    isCodes:'/api/member/isTrueCodes.do',
    codeUrl: '/api/member/getCodes.do',
	action:"",
	submit:"{{submit}}"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      password: options.password,
      mobile: options.mobile
    })
	  if(this.data.action){
		  var $this = this;
		  ux.request(this.data.action,options,function(protocol){
				$this.setData(protocol.data);
		  });
	  }    
  },
  
  onForm:function(event){
	if(this.data.submit){
		  var $this = this;
		  var dataset = event.currentTarget.dataset;
		  ux.request(this.data.submit,detail.value,function(protocol){
				if(protocol.success){
					if(dataset.url){
						wx.redirectTo(dataset.url);
					}
				}
		  },true);
	 }
  },
  
  onTap:function(event){
	  var dataset = event.currentTarget.dataset;
	  var data = this.data;
	  if(dataset.key && dataset.val){
		  data[dataset.key] = dataset.val;
	  }
	  this.setData(data);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  codes:function(e){
  this.setData({
    code:e.detail.value
  })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
  },
  toast: function (params) {
    if (typeof (params) == "string") {
      params = { content: params };
    }
    this.setData({
      toast: params.content
    });
    var THIS = this;
    setTimeout(function () {
      THIS.setData({
        toast: false
      });
      if (params.complete) {
        params.complete();
      }
    }, params.time || 3000);
  },
  verifyCodeEvent: function (e) {
    if (this.data.buttonDisable) return false;
    var that = this;
    var mobile = this.data.mobile;
    if (mobile == '') {
      this.toast('请先输入手机号码！');
      return false;
    }
    var regMobile = /^1\d{10}$/;
    if (!regMobile.test(mobile)) {
      this.toast('手机号有误！');
      return false;
    }
    that.setData({
      buttonDisable: true
    })
    var c = 120;
    var intervalId = setInterval(function () {
      c = c - 1;
      that.setData({
        verifyCodeTime: c + '秒后重发',
        buttonDisable: true
      })
      if (c == 0) {
        clearInterval(intervalId);
        that.setData({
          verifyCodeTime: '获取验证码',
          buttonDisable: false
        })
      }
    }, 1000)
    var param = { mobile: mobile };
    var $this = this;
    ux.request(this.data.codeUrl, param, function (protocol) {
      wx.showToast({
        title: '请注意查收！'
      })
    }, true);
  },
  toSelectAdderss:function(){
    if(!this.data.code){
     this.toast("请输入验证码");
     return false;
    } 
    var self=this;
    var param = { mobile: self.data.mobile, code: self.data.code}
    ux.request(this.data.isCodes, param, function (protocol) {
      if(protocol.data.type==1){
        wx.navigateTo({
          url: '/pages/distributor/tab3/auth/selectAdress?mobile=' + self.data.mobile + '&code=' + self.data.code + "&password=" + self.data.password,
        })
      }else{
        self.toast("验证码有误!");
        return false;
      }
    }, true);
  },
  look:function(){
    wx.navigateTo({
      url: '/pages/protocol/psserver',
    })
  }
})
